<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 资金账户模型
 * @author www.dingwei.cn
 */

class ConsumerBuyModel extends MY_Model{
    public $tbUser = 'consumer';
    public $tbOrder = 'consumer_order';
    public $tbBuyLog = 'consumer_buy_log';
    public $config = array(
        'type'=>array('1' => '充值','2' => '支付','3' => '退款','4' => '提现','5' => '领取红包','6' => '平台调整','7' => '分享收益'),
        'pay_type'=>array('1' => '余额','2' => '微信','3' => '支付宝','4' => '银行卡','5'=>'商盟红包','6' => '平台调整','7' => '分享收益'),
        'status'=>array('1' => '失败','2' => '成功','3' => '申请中'),
    );

    /**
     * 保存
     *
     * @param	array	$data	保存数据
     * @param	integer $id		用户id
     * @return	boolean
     */
    public function save($data, $id = 0){

        if( ! $data) return false;
        if($id){
            $this->db->where(array('id' => $id));
            if($this->update($this->tbBuyLog, $data)){
                return true;
            }
        }else{
            if($id = $this->insert($this->tbBuyLog, $data)){
                return $id;
            }
        }
    }

    /**
     * 后台获取列表
     *
     * @return array
     */
	public function adminGetList(){

        $consumerId = intval(get('consumer_id'));
        $type = intval(get('found_type'));
        $status = intval(get('found_status'));
        $keyword = trim(get('keyword'));
        $amount = trim(get('amount'));
        $confirm_time_from = strtotime(get('confirm_time_from'));
        $confirm_time_to = strtotime(get('confirm_time_to'));

        $addtime_from = strtotime(get('addtime_from'));
        $addtime_to = strtotime(get('addtime_to'));
        $distribution = intval(get('distribution'));
        $iSortCol_0 = get('iSortCol_0');
        $sSortDir_0 = get('sSortDir_0');

        if ($iSortCol_0 == 1){
            $this->db->order_by('b.amount '.$sSortDir_0);
        }

        if ($iSortCol_0 == 3){
            $this->db->order_by('b.addtime '.$sSortDir_0);
        }

        if($type){
            $this->db->where('b.type', $type);
        }

        if($status){
            $this->db->where('b.status', $status);
        }
        if ($consumerId){
            $this->db->where(array('b.consumer_id' => $consumerId));
        }
        if ($keyword){
            $this->db->where('(u.nickname like "%'.$keyword.'%" or u.mobile like "%'.$keyword.'%")');
        }
        if ($amount){
            $this->db->like(array('b.amount' => $amount));
        }
        if($confirm_time_from){
            $this->db->where('b.confirm_time >=', $confirm_time_from);
        }
        if($confirm_time_to){
            $this->db->where('b.confirm_time <= ', strtotime('+1day', $confirm_time_to));
        }
        if($addtime_from){
            $this->db->where('b.addtime >=', $addtime_from);
        }
        if($addtime_to){
            $this->db->where('b.addtime <= ', strtotime('+1day', $addtime_to));
        }

        $select = 'b.id,b.type,b.pay_type,b.amount,b.status,b.reason,b.addtime,b.confirm_time,u.mobile,u.nickname';
        if($distribution){
            $select .= ',p.mobile as child_mobile, count(bl.id) as bonusCount';
            $this->db->join($this->tbOrder.' o','o.id = b.param')
                    ->join('consumer_bonus_log bl','o.id = bl.param and bl.type = 1', 'left')
                    ->join($this->tbUser.' p','p.id = o.consumer_id');
        }
	    $this->db->select($select)
                 ->join($this->tbUser.' u','u.id = b.consumer_id')
                ->group_by('b.id')
                 ->order_by('b.addtime desc');

        return $this->pagination($this->tbBuyLog.' b');

    }

    /**
     * 获取数据详情
     * @param $id
     * @return array
     * @author heyu
     */
    public function getInfo($id){
        if (!$id){
            return array();
        }
        $this->db->where(array('id' => $id));
        $tA = $this->getOne($this->tbBuyLog);
        return $tA;
    }

    /**
     * 商盟红包领取统计
     * @param $id      红包id
     * @return array
     */
    public function getMerchantBonusStatistics($id){
        if(!$id)return array();

        $this->db->select('count(id) as total,sum(amount) as amount')
                 ->where(array('related_id' => $id,'type' => 5));

        return $this->getOne($this->tbBuyLog);
    }

    /**
     * 商盟红包领取记录
     * @param $id
     * @return array
     */
    public function getMerchantBonusLog($id, $page){
        if(!$id)return array();

        $this->db->select('l.amount,l.addtime,u.nickname')
            ->join($this->tbUser.' u', 'u.id = l.consumer_id')
            ->where(array('l.related_id' => $id,'l.type' => 5));

        $tA = $this->appPage($this->tbBuyLog.' l', $page);
        foreach ($tA['list'] as $k=>$v){
            $tA['list'][$k]['addtime'] = dateFormat($v['addtime']);
        }

        return $tA['list'];
    }

    /**
     * 获取提现总金额 总次数
     * @param int $startTime
     * @param int $endTime
     * @param bool $isOne
     * @param int $page
     * @param int $pageNumber
     * @return array
     * @author heyu
     */
    public function getTotalBuyLog($startTime = 0, $endTime = 0, $isOne = true, $page = 0, $pageNumber = 0){
        if ($startTime){
            $this->db->where('addtime >= '.$startTime);
        }
        if ($endTime){
            $this->db->where('addtime < '.$endTime);
        }
        $this->db->select('sum(if(type=3, amount, null)) as refund,sum(if(type=4, amount, null)) as withdrawals
            ,FROM_UNIXTIME(addtime,"%Y-%m-%d") as time')
            ->where(array('status' => 2))
            ->where_in('type', array(3,4));
        if ($isOne){
            return $this->getOne($this->tbBuyLog);
        }else{
            $this->db->group_by('time')
                ->order_by('time desc');
            $data = array();
            if ($page && $pageNumber) {
                $tA = $this->appPage($this->tbBuyLog, $page, false, $pageNumber);
                if (!empty($tA['list'])){
                    foreach ($tA['list'] as $v){
                        $data[$v['time']] = $v;
                    }
                }
            }else {
                $tA = $this->pagination($this->tbBuyLog);
                if (!empty($tA['data'])){
                    foreach ($tA['data'] as $v){
                        $data[$v['time']] = $v;
                    }
                }
            }

            unset($tA);
            return $data;
        }



    }
}